//
// Created by bleedingfight on 2020/6/25.
//
#include "dailyTemperatures.h"

std::vector<int> dailyTemperatures(std::vector<int>& T){
    std::vector<int>result = std::vector<int>(T.size(),0);
    std::stack<int> min_stack;
    for(int i=0;i<T.size();i++){
        if(min_stack.empty()){
            min_stack.push(i);
        }else{
            int top_index = min_stack.top();
            if (T[i]<=T[top_index]){
                min_stack.push(i);

            }else{
                while (!min_stack.empty() && T[i]>T[min_stack.top()]){
                    result[min_stack.top()]=i-min_stack.top();
                    min_stack.pop();
                }
                min_stack.push(i);

            }
        }
    }
    return result;
}
